$(document).ready(function(){
	var cdId = $("#cdId");
	var isCdId = ($(cdId).length > 0) ? true : false;
	if(isCdId) codeTypeChange();
	
	$("#jqwCloseBtn").on("click", function(){
		var jqw = new jqWindow();
		jqw.destroyJqwSelected();
	});

	$("#jqwFormReviseSaveBtn").on("click", function(){
		if(!validationForm()) return;

		var chkFields = new Object();
		chkFields["actionType"] = "revise";
		chkFields["cdId"] = $("#cdId").val();
		chkFields["fnId"] = $("#fnId").val();
		chkFields["fnAlias"] = $("#fnAlias").val();
		checkDuplication.existFnId(chkFields, dwrResult);
	});
	
	$("#jqwFormSaveBtn").on("click", function(){
		if(!validationForm()) return;

		var chkFields = new Object();
		chkFields["actionType"] = "new";
		chkFields["cdId"] = $("#fnId").val();
		chkFields["fnId"] = $("#fnId").val();
		chkFields["fnAlias"] = $("#fnAlias").val();
		checkDuplication.existFnId(chkFields, dwrResult);
	});
	
	$("#codeTypes").on("change", codeTypeChange);
	
	$("#fnHighestLevlYN").on("change", function(){
		if(isCdId && $(this).is(":checked")){
			var chkFields = new Object();
			chkFields["cdId"] = $(cdId).val();
			checkInternalCode.getUsedLowLevelGrpFnList(chkFields, dwrUsedCodeGroupList);
		}
	});
});

function dwrUsedCodeGroupList(data){
	if(data != null && data != "undefined"){
		var rtnValue = "";
		var jHelper = new jsonHelper2();
		var json = jHelper.stringToJsonObj(data);

		var cnt = 0;
		var xWindow = new Browser();
		if(xWindow.isIE && xWindow.version < 9.0){
			for(key in json) {
				var item = json[key];
				rtnValue += (++cnt) + ". ["+ item.grpAlias +"]의 "+ "["+ item.fnAlias +"]기능의 하위에서 사용중.\n";
			}
			
		}else{
			Object.keys(json).forEach(function(key){
				var item = json[key];
				rtnValue += (++cnt) + ". ["+ item.grpAlias +"]의 "+ "["+ item.fnAlias +"]기능의 하위에서 사용중.\n";
			});
		}
		
		alert("최상위 레벨로 지정할 수 없습니다.\n\n"+rtnValue);
		$("#fnHighestLevlYN").attr("checked", false);
	}
}

function codeTypeChange(){
	var codeTypeObj = $("#codeTypes");
	var codeObj = $("#fnId");
	var cdId = $("#cdId");
	var isCdId = ($(cdId).length > 0) ? true : false;
	
	$(codeObj).children("option").not(":first").remove();
	
	if($(codeTypeObj).children("option:selected").val() == ""){
		return;
	}

	var chkFields = new Object();
	chkFields["actionType"] = "new";
	
	$(codeTypeObj).children("option:selected").each(function(idx, item){
		if(isCdId) {
			chkFields["actionType"] = "revise";
			chkFields["cdId"] = $(cdId).val();
		}
		chkFields["cdTpId"] = $(item).val();
		
		checkInternalCode.getNotUsedCodeList(chkFields, dwrNotUsedCodeList);
	});
}

function dwrNotUsedCodeList(data){
	var codeObj = $("#fnId");
	var cdId = $("#cdId");
	var isCdId = ($(cdId).length > 0) ? true : false;
	
	if(data != null && data != "undefined"){
		var jHelper = new jsonHelper2();
		var json = jHelper.stringToJsonObj(data);

		var xWindow = new Browser();
		if(xWindow.isIE && xWindow.version < 9.0){
			for(key in json) {
				var item = json[key];
				var itemSel = (isCdId && $(cdId).val() == item.cdId) ? "selected='selected'" : "";
				var option = "<option value='"+item.cdId+"' "+itemSel+">"+item.cdAlias + "("+item.cdId+")</option>";
				$(codeObj).append($(option));
			}
			
		}else{
			Object.keys(json).forEach(function(key){
				var item = json[key];
				var itemSel = (isCdId && $(cdId).val() == item.cdId) ? "selected='selected'" : "";
				var option = "<option value='"+item.cdId+"' "+itemSel+">"+item.cdAlias + "("+item.cdId+")</option>";
				$(codeObj).append($(option));
			});
		}
		
	}else{
		var option = "<option value=''>Nothing code for using</option>";
		$(option).appendTo($(codeObj));
	}
}

function dwrResult(data){
	var jHelper = new jsonHelper2();
	var json = jHelper.stringToJsonObj(data);

	if(!json.result){
		var url = "/template/system/function/"+((json.actionType == "new")?"_regFormSave":"_regFormReviseSave");
		var options = new Object();
		options["url"] = url;
		jqwSave(options);
		
	}else{
		alert(json.msg);
		$("#"+json.chkField).focus();
		return;
	}
}

function validationForm(){
	var def = new validDefinition();
	var validator = new formValidator("frmFnReg", def.rules, def.messages);
	return validator.validation();
}

function jqwSave(opts){
	var obj = $(".pageNavi").find(".numberingOn");
	naviNumClickEventReg(obj);

	var options = new Object();
	options["url"] = opts["url"];
	options["type"] = "POST";
	options["dataType"] = "json";
	options["data"] = "frm:frmFnReg";	//Available no writing this option OR pram1=?&param2=? OR frm:form Id
	options["triggerObj"] = obj;	//트리 메뉴에서 현재 화면을 triggering(to click) 할 수 있는 오브젝트.
	options["jqwType"] = "save";	//< "popup" | "save" | [null | ""] >
	options["jqwClose"] = true;
	
	var jqw = new jqWindow();
	jqw.jqwSet(options);
	jqw.loadJqWindow();
}
